/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  BuildAddActivity.java   
 * @Package com.jiyi.activity.owner   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-9-11 下午1:35:34   
 * @version V1.0     
 */
package com.jiyi.activity.new_maintenance;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.easemob.util.UserHelp;
import com.jiyi.event.BuildListEvent;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.CheckUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: BuildAddActivity
 * @Description:TODO(新增大楼)
 * @author: HYM
 * @date: 2017-9-11 下午1:35:34
 * 
 */
public class BuildAddActivity extends ActivityBase {

	/* 头部 */
	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.etBuildNo)
	EditText etBuildNo;
	@BindView(R.id.etBuildRemark)
	EditText etBuildRemark;
	@BindView(R.id.etHost)
	EditText etHost;
	@BindView(R.id.etBrand)
	EditText etBrand;
	@BindView(R.id.etModel)
	EditText etModel;
	@BindView(R.id.etSpecification)
	EditText etSpecification;
	@BindView(R.id.etRemark)
	EditText etRemark;
	@BindView(R.id.etCreator)
	EditText etCreator;

	String companyId;
	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_build_add);
		ButterKnife.bind(this);
		init();
	}

	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-9-12
	 */
	private void init() {
		getIntentData();
		setTop();
		setViewState();
	}

	private void setViewState() {
		// 设置必填项目验证
		etBuildNo.setTag(R.id.tag_view_empty, "楼号不能为空");
		etHost.setTag(R.id.tag_view_empty, "主机名称不能为空");
		etBrand.setTag(R.id.tag_view_empty, "品牌不能为空");
		etModel.setTag(R.id.tag_view_empty, "型号不能为空");
		etCreator.setTag(R.id.tag_view_empty, "厂家不能为空");
	}

	/**
	 * @author: HYM
	 * @Title: getIntentData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-9-14
	 */
	private void getIntentData() {
		companyId = getIntent().getStringExtra("companyId");
	}

	private void setTop() {
		tvTitle.setText("新增大楼");
		tvSearch.setCompoundDrawables(null, null, null, null);
		tvSearch.setText("取消");
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch, R.id.tvPost })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tvBack:
		case R.id.tvSearch:
			finish();
			break;
		case R.id.tvPost:
			// 执行新增操作
			if (CheckUtil.checkView(etBuildNo, etHost, etBrand,etModel, etCreator)) {
				doPost();
			}
			break;
		default:
			break;
		}
	}

	void doPost() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN,
				UserInfoUtil.getCurrentUserAccessToken());
		params.put("company_id", companyId);
		params.put("building", etBuildNo.getText().toString());
		params.put("describe", etBuildRemark.getText().toString());
		params.put("equipment_name", etHost.getText().toString());
		params.put("equipment_brand", etBrand.getText().toString());
		params.put("equipment_model", etModel.getText().toString());
		params.put("specifications", etSpecification.getText().toString());
		params.put("manufacture_factory", etCreator.getText().toString());
		params.put("remark", etRemark.getText().toString());

		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this,
				WebApi.MAINTENANCE_OWNER_BUILD_ADD, "", params,
				new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								EventBus.getDefault()
										.post(new BuildListEvent());
								finish();
							} else if (status.equals("404")
									&& info.contains("尚未登录")) {
								ToastUtil.getInstances().showMsg(
										"尚未登录或该用户在其他地方登录，请重新登录！");
								// 清除环信登录
								UserHelp.getInstance().clearUser(
										AppBase.Context, true);
								return;
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(
								R.string.err_http_response);
					}
				});
	}
}
